package com.lizheng.yunservice.goods.controller;

import com.lizheng.yunservice.common.domain.ApiRestResponse;
import com.lizheng.yunservice.common.helper.LoginHelper;
import com.lizheng.yunservice.goods.model.VtpCustomerLikesDTO;
import com.lizheng.yunservice.goods.service.VtpCustomerLikesService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/vtp/app/customerLikes")
@AllArgsConstructor
public class VtpAppCustomerLikesController {
    // 注入接口
    private VtpCustomerLikesService vtpCustomerLikesService;

    /**
     * 处理点赞请求的公共方法
     * @param dto 点赞数据传输对象
     * @return ApiRestResponse 操作结果
     */
    private ApiRestResponse<String> handleLikesRequest(VtpCustomerLikesDTO dto) {
        // 1. 验证登录状态
        if (!LoginHelper.isLogin()) {
            return ApiRestResponse.fail("请先登录");
        }

        // 2. 获取用户信息
        Long userId = LoginHelper.getUserId();
        String username = LoginHelper.getUsername();
        log.debug("收到点赞操作请求{}, 用户ID: {}, 用户名: {}", dto, userId, username);
        return dto.getObjectType()!=null
                ? vtpCustomerLikesService.saveWxLikes(dto, userId, username)
                : vtpCustomerLikesService.cancelWxLikes(dto, userId, username);

    }

    /**
     * 保存微信用户的点赞
     * @param dto 点赞数据导入类
     * @return ApiRestResponse 统一响应类
     */
    @PostMapping("/saveWxLikes")
    public ApiRestResponse<String> saveWxLikes(@RequestBody VtpCustomerLikesDTO dto) {
        return handleLikesRequest(dto); //调用公共方法
    }

    @PostMapping("/cancelWxLikes")
    public ApiRestResponse<String> cancelWxLikes(@RequestBody VtpCustomerLikesDTO dto) {
        return handleLikesRequest(dto); //调用公共方法
    }


}